//
//  EntertainmentView.m
//  ToWhere
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "EntertainmentView.h"

//按钮的宽高
#define kButtonHeight self.frame.size.height
#define kButtonWidth  self.frame.size.width / 2

#define kTintColor [UIColor orangeColor]

@interface EntertainmentView()

//按钮的表示条
@property(nonatomic, strong) UIView *lineView;

@end

@implementation EntertainmentView

-(instancetype)initWithFrame:(CGRect)frame Array:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.buttonTitleArray = [NSArray arrayWithArray:array];
    }
    return self;
}

-(void)setButtonTitleArray:(NSArray *)buttonTitleArray
{
    if (_buttonTitleArray != buttonTitleArray) {
        _buttonTitleArray = buttonTitleArray;
    }
    
    UIScrollView *btnScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 2)];
    
    //设置btnScrollView的contentSize
    btnScrollView.contentSize = CGSizeMake(kButtonWidth * _buttonTitleArray.count, 0);
    btnScrollView.showsHorizontalScrollIndicator = NO;
    
    for (int i = 0; i < buttonTitleArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(kButtonWidth * i, 0, kButtonWidth, kButtonHeight - 2);
        button.titleLabel.font = [UIFont systemFontOfSize:18 * kWidth];
        [button setTitle:_buttonTitleArray[i] forState:UIControlStateNormal];
        if (i == 0) {
            [button setTitleColor:kTintColor forState:UIControlStateNormal];
        }
        else
        {
            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        button.tag = i + 5000;
        [btnScrollView addSubview:button];
        
    }
    //横线
    self.lineView = [[UIView alloc]initWithFrame:CGRectMake(50 * kWidth, kButtonHeight - 4, kButtonWidth - 100 * kWidth, 3)];
    self.lineView.backgroundColor = kTintColor;
    [btnScrollView addSubview:self.lineView];
    btnScrollView.tag = 100;
    [self addSubview:btnScrollView];
}

-(void)btnAction:(UIButton *)button
{
    for (UIButton *btn in [self viewWithTag:100].subviews) {
        
        if([btn isKindOfClass:[UIButton class]])
        {
            if (button.tag == btn.tag) {
                [btn setTitleColor:kTintColor forState:UIControlStateNormal];
            }
            else
            {
                [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            }
        }
    }
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.lineView.frame = CGRectMake(button.frame.origin.x + 50 * kWidth, kButtonHeight - 4, kButtonWidth - 100 * kWidth, 3);
        
    }];
    
    //执行代理时刻
    [self.delegate buttonOfScrollLineClicked:button];
}

@end
